// com.example.aftersales.controller.AnnouncementController.java
package com.example.aftersales.controller;

import com.example.aftersales.entity.Announcement;
import com.example.aftersales.service.AnnouncementService;
import com.example.aftersales.vo.ServiceResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/api/announcements")
public class AnnouncementController {

    @Resource
    private AnnouncementService announcementService;

    @GetMapping
    public ServiceResult<List<Announcement>> list() {
        List<Announcement> announcements = announcementService.listAll();
        return ServiceResult.success(announcements);
    }

    @PostMapping
    public ServiceResult<Boolean> add(@RequestBody Announcement announcement) {
        announcement.setPublishDate(LocalDate.now());
        boolean result = announcementService.add(announcement);
        if (result) {
            return ServiceResult.success(true, "公告发布成功");
        } else {
            return ServiceResult.fail("公告发布失败");
        }
    }

    @PutMapping
    public ServiceResult<Boolean> update(@RequestBody Announcement announcement) {
        boolean result = announcementService.update(announcement);
        if (result) {
            return ServiceResult.success(true, "公告更新成功");
        } else {
            return ServiceResult.fail("公告更新失败");
        }
    }

    @DeleteMapping("/{id}")
    public ServiceResult<Boolean> delete(@PathVariable Long id) {
        boolean result = announcementService.delete(id);
        if (result) {
            return ServiceResult.success(true, "公告删除成功");
        } else {
            return ServiceResult.fail("公告删除失败");
        }
    }
}